package com.atguigu.flink.charkoer10;

import com.atguigu.flink.been.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.client.program.StreamContextEnvironment;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class FlinkKeyStateReduce {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamContextEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        env.socketTextStream("hadoop162", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0], Long.valueOf(split[1]), Integer.valueOf(split[2]));
                    }
                })
                .keyBy(WaterSensor::getId)
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {
                    private ReducingState<WaterSensor> vcReduce;

                    @Override
                    public void open(Configuration parameters) throws Exception {

                        vcReduce = getRuntimeContext()
                                .getReducingState(
                                        new ReducingStateDescriptor<WaterSensor>("vcReduce",
                                                new ReduceFunction<WaterSensor>() {
                                                    @Override
                                                    public WaterSensor reduce(WaterSensor value1, WaterSensor value2) throws Exception {
                                                        value1.setVc(value1.getVc() + value2.getVc());
                                                        return value1;
                                                    }
                                                },
                                                WaterSensor.class)
                                );
                    }

                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                        vcReduce.add(value);

                        out.collect(ctx.getCurrentKey() + " 的水位和是:" + vcReduce.get());

                    }
                }).print();

        env.execute();
    }
}
